<?php

error_reporting(E_ERROR | E_PARSE);

class ConnectionIntra
{
  private	$login;
  private	$password;
  private	$cookies;

  private	$fullName;
  private	$promo;
  private	$city;

  private	$isConnected = false; /** bool */
  private	$error; /** 0 == unknwon login || 1 == wrong password **/

  private function login()
  {
    $post = array(
		  'action'  => 'login',
		  'login'   => $this->login,
		  'passwd'  => $this->password
		  );
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://www.epitech.eu/intra/');
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookies);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    $result = curl_exec($ch);
    if (strstr($result, "Identification Failed"))
      {
	$this->isConnected = false;
	if (strstr($result, "Identification Failed (Unknown login )"))
	  $this->error = 0;
      	else if (strstr($result, "Identification Failed (password doesn't match) ..."))
	  $this->error = 1;
      }
    else
      $this->isConnected = true;
    curl_close($ch);
  }
  
  private function parseFullName($rapport)
  {
    try
      {
	$doc = new DOMDocument();
	$doc->loadHTML($rapport);
	
	$container = $doc->getElementById('intra_display_zone');
	$trs = $container->getElementsByTagName('table')->item(1)->getElementsByTagName('tr');
		
	$this->fullname = $trs->item(2)->getElementsByTagName('td')->item(1)->nodeValue;
	$promo = $trs->item(4)->getElementsByTagName('td')->item(1)->nodeValue;	
	$city = $trs->item(5)->getElementsByTagName('td')->item(1)->nodeValue;
	
	$array = explode(" ", trim($promo));
	$this->promo = $array[1];
	
	$array = explode(" ", trim($city));
	$this->city = $array[1];
      }
    catch(Execption $e)
      {
	echo "Error parsing rapport student !\n";
	return (false);
      }
    return (true);
  }
  
  public function	parseRapportStudent()
  {
    $post = array(
		  'page'       => 'rapport',
		  'login'       => $this->login,
		  );
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://www.epitech.eu/intra/index.php?section=etudiant');
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookies);
    
    $rapport = curl_exec($ch);
    return ($this->parseFullName($rapport));
  }
    
  /*
   **
   **   GETTER / SETTER
   **
   */
  
  public function getPromo()
  {
    return ($this->promo);
  }

  public function getCity()
  {
    return ($this->city);
  }
  
  public function getFullName()
  {
    return ($this->fullname);
  }

  public function checkIsConnected()
  {
    return ($this->isConnected);
  }
  
  public function	checkProblemForConnection()
  {
    return ($this->error);
  }
  
  /*
   **
   **      CONSTRUCTEUR (LOGIN, PASSWORD)
   **      DESTRUCTOR
   */
  
  function __destruct()
  {
    unlink($this->cookies);
  }
  
  function __construct($login, $password)
  {
    if (isset($login) && isset($password))
      {
	$this->login = $login;
	$this->password = $password;
	$this->cookies = "/tmp/" . "cookies_" . uniqid();
	$this->login();
      }
  }
}

?>